<template>
  <component
    is="a-modal"
    v-model:visible="visible"
    :width="600"
    :title="title"
    :mask-closable="false"
    :ok-loading="loading"
    @cancel="close"
    @before-ok="submit">
    <!-- 表单信息 start -->
    <a-form ref="formRef" :model="formData" :rules="rules" :auto-label-width="true">
      <a-form-item label="项目id" field="project_id">
        <a-input v-model="formData.project_id" placeholder="请输入项目id" />
      </a-form-item>
      <a-form-item label="会议名称" field="meeting_name">
        <a-input v-model="formData.meeting_name" placeholder="请输入会议名称" />
      </a-form-item>
      <a-form-item label="确认邀请" field="confirm_invitation">
        <a-input v-model="formData.confirm_invitation" placeholder="请输入确认邀请" />
      </a-form-item>
      <a-form-item label="邀请总数" field="total_invitation">
        <a-input v-model="formData.total_invitation" placeholder="请输入邀请总数" />
      </a-form-item>
      <a-form-item label="状态" field="status">
        <a-select v-model="formData.status" :options="[]" placeholder="请选择状态" allow-clear />
      </a-form-item>
      <a-form-item label="前端是否显示" field="is_display">
        <a-input v-model="formData.is_display" placeholder="请输入前端是否显示" />
      </a-form-item>
    </a-form>
    <!-- 表单信息 end -->
  </component>
</template>

<script setup>
import { ref, reactive, computed } from 'vue'
import { Message, Modal } from '@arco-design/web-vue'
import api from '../api/meeting'

const emit = defineEmits(['success'])
// 引用定义
const visible = ref(false)
const loading = ref(false)
const formRef = ref()
const mode = ref('')

let title = computed(() => {
  return '项目会议' + (mode == 'add' ? '-新增' : '-编辑')
})

// 表单信息
const formData = reactive({
  id: null,
  project_id: null,
  meeting_name: '',
  confirm_invitation: null,
  total_invitation: null,
  status: 1,
  is_display: null,
})

// 验证规则
const rules = {
  project_id: [{ required: true, message: '项目id必需填写' }],
  meeting_name: [{ required: true, message: '会议名称必需填写' }],
}

// 打开弹框
const open = async (type = 'add') => {
  mode.value = type
  visible.value = true
  formRef.value.resetFields()
  await initPage()
}

// 初始化页面数据
const initPage = async () => {}

// 设置数据
const setFormData = async (data) => {
  for (const key in formData) {
    if (data[key] != null && data[key] != undefined) {
      formData[key] = data[key]
    }
  }
}

// 数据保存
const submit = async (done) => {
  const validate = await formRef.value?.validate()
  if (!validate) {
    loading.value = true
    let data = { ...formData }
    let result = {}
    if (mode.value === 'add') {
      // 添加数据
      data.id = undefined
      result = await api.save(data)
    } else {
      // 修改数据
      result = await api.update(data.id, data)
    }
    if (result.code === 200) {
      Message.success('操作成功')
      emit('success')
      done(true)
    }
    // 防止连续点击提交
    setTimeout(() => {
      loading.value = false
    }, 500)
  }
  done(false)
}

// 关闭弹窗
const close = () => (visible.value = false)

defineExpose({ open, setFormData })
</script>
